/**
 * Moondance UML modeling tool
 * 
 * This program is free software; you can redistribute it and
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 3
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

package ro.pub.cs.se.moondance.command;

import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.commands.Command;

import ro.pub.cs.se.moondance.model.ModelElement;
import ro.pub.cs.se.moondance.model.UMLDiagram;
import ro.pub.cs.se.moondance.model.UMLStateMachine;

/**
 * A command to add a Shape to a ShapeDiagram. The command can be undone or
 * redone.
 */
public class StateMachineCreateCommand extends Command {

	/** The new package. */
	private UMLStateMachine _machine;
	/** UMLDiagram to add to. */
	private final ModelElement parent;
	/** The bounds of the new Shape. */
	private Rectangle bounds;

	/**
	 * Create a command that will add a new StateMachine to a UMLDiagram.
	 * 
	 * @param _machine
	 *            the new StateMachine that is to be added
	 * @param parent
	 *            the UMLDiagram that will hold the new element
	 * @param bounds
	 *            the bounds of the new StateMachine; the size can be (-1, -1)
	 *            if not known
	 * @throws IllegalArgumentException
	 *             if any parameter is null, or the request does not provide a
	 *             new Shape instance
	 */
	public StateMachineCreateCommand(UMLStateMachine _machine,
			ModelElement parent, Rectangle bounds) {
		this._machine = _machine;
		this.parent = parent;
		this.bounds = bounds;
		setLabel("StateMachine creation");
	}

	/**
	 * Can execute if all the necessary information has been provided.
	 * 
	 * @see org.eclipse.gef.commands.Command#canExecute()
	 */
	public boolean canExecute() {
		return _machine != null && parent != null && bounds != null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.gef.commands.Command#execute()
	 */
	public void execute() {
		_machine.setLocation(bounds.getLocation());
		Dimension size = bounds.getSize();
		if (size.width > 0 && size.height > 0)
			_machine.setSize(size);

		Object genericParent = (Object) parent;
		if (genericParent instanceof UMLDiagram)
			_machine.setName("StateMachine"
					+ (((UMLDiagram) genericParent).getChildren().size() + 1));
		_machine.setParent(parent);

		redo();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.gef.commands.Command#redo()
	 */
	public void redo() {
		Object genericParent = (Object) parent;
		if (genericParent instanceof UMLDiagram)
			((UMLDiagram) genericParent).addChild(_machine);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.gef.commands.Command#undo()
	 */
	public void undo() {
		Object genericParent = (Object) parent;
		if (genericParent instanceof UMLDiagram)
			((UMLDiagram) genericParent).removeChild(_machine);
	}
}
